package com.laputacastle.dr.service.impl;

import com.laputacastle.dr.mapper.DrCatalogMapper;
import com.laputacastle.dr.model.DrCatalog;
import com.laputacastle.dr.service.DrCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DrCatalogServiceImpl implements DrCatalogService {

    @Autowired
    private DrCatalogMapper drCatalogMapper;

    @Override
    public List<Map<String, Object>> getCatalogTreeData() {
        List<DrCatalog> drCatalogs = drCatalogMapper.getCatalogTreeData();
        return getTreeJson(drCatalogs);
    }

    /**
     * getTreeJson
     * @param drCatalogs
     * @return
     */
    public List<Map<String, Object>> getTreeJson (List<DrCatalog> drCatalogs) {
        List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
        Map<String, Object> tree = new  HashMap<String, Object>();
        for (DrCatalog drCatalog : drCatalogs) {
            tree = new HashMap<String, Object>();
            if ("0".equals(drCatalog.getCPid())) {
                String pid = drCatalog.getCId();
                int pDepth = drCatalog.getDepth();
                tree.put("id", pid);
                tree.put("name", drCatalog.getCName());
                tree.put("menuName", drCatalog.getCMenuName());
                tree.put("iconUrl", drCatalog.getCImgurl());
                List<Map<String, Object>> children = getChildrenNode(drCatalogs, pid, pDepth);
                if (children.size() != 0) {
                    tree.put("items", children);
                } else {
                    children = new ArrayList<>();
                    tree.put("items", children);
                }
                treeList.add(tree);
            }

        }
        return treeList;
    }

    /**
     * getChildrenNode
     * @param drCatalogs
     * @param pid
     * @param pDepth
     * @return
     */
    public List<Map<String, Object>> getChildrenNode (List<DrCatalog> drCatalogs, String pid, int pDepth) {
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new  HashMap<String, Object>();
        List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
        for (DrCatalog drCatalog : drCatalogs) {
            if (drCatalog.getCPid().equals(pid)) {
                map = new  HashMap<String, Object>();
                // 递归获取子节点
                String id = drCatalog.getCId();
                int depth = drCatalog.getDepth();
                map.put("id", id);
                map.put("name", drCatalog.getCName());
                map.put("menuName", drCatalog.getCMenuName());
                map.put("iconUrl", drCatalog.getCImgurl());
                children = getChildrenNode(drCatalogs, id, depth);
                if (children.size() != 0) {
                    map.put("items", children);
                } else {
                    children = new ArrayList<>();
                    map.put("items", children);
                }
                lists.add(map);
            }
        }
        return lists;
    }

}
